<?php
// +----------------------------------------------------------------------
// | title: 标题注释
// +----------------------------------------------------------------------
// | Author: TZ.Leong
// +----------------------------------------------------------------------
// | Date: 2020/7/8 11:10
// +----------------------------------------------------------------------

namespace app\single\controller;


use app\single\model\PayTotalModel;
use app\single\model\RoleModel;
use cmf\controller\AdminBaseController;
use think\Db;
use think\exception\DbException;

class DataAnalysisController extends AdminBaseController
{
    /**
     * 闯关数据分析
     * @param RoleModel $roleModel
     * @param PayTotalModel $payTotalModel
     * @return mixed
     */
    public function statHook(RoleModel $roleModel, PayTotalModel $payTotalModel)
    {
        $sDay = date("Ymd", strtotime(input("sDay", date("Y-m-d"))));
        if($sDay == date("Ymd")){
            //查询用户通关数据
            $list = Db::table(config("database.game").".role")
                ->alias("rh")
                ->leftJoin(["role_pay_total" => "rp"], "rp.role_id = rh.id")
                ->where("cur_id", ">", 0)
                ->group("cur_id")
                ->column("COUNT(rh.id) AS num, COUNT(rp.role_id) AS pay_num", "cur_id");
        }else{
            $list = Db::table("c_stat_hook")
                ->where("day", $sDay)
                ->column("num, pay_num", "level");
        }
        $keys = array_keys($list);
        $fillArr = array_fill(1, end($keys), 0);
        array_walk($list, function(&$v, $k) use ($fillArr, &$data){
            foreach($fillArr as $i => $vo){
               if($i <= $k){
                   if(isset($data[$i])){
                       $data[$i]["num"] += $v["num"];
                       $data[$i]["pay_num"] += $v["pay_num"];
                   }else{
                       $data[$i] = $v;
                   }
               }else{
                   $data[$i]["num"] = 0;
                   $data[$i]["pay_num"] = 0;
               }
            }
        });
        $this->assign("data", $data);
        $this->assign("total", $roleModel->count("id"));
        $this->assign("payTotal", $payTotalModel->count("role_id"));
        return $this->fetch();
    }

    /**
     * 充值玩家闯关层数
     * @param PayTotalModel $payTotalModel
     * @return mixed
     * @throws DbException
     */
    public function payerHook(PayTotalModel $payTotalModel)
    {
        $list = $payTotalModel
            ->alias("rp")
//            ->join([config("database.game").".role_hook" => "rh"], "rh.role_id = rp.role_id")
            ->join([config("database.game").".role" => "r"], "r.id = rp.role_id")
            ->field("rp.role_id,r.cur_id AS level,rp.rmb,r.name")
            ->order("level desc")
            ->paginate(self::$pageSize);
        $list->appends($this->request->param());
        $this->assign("data", $list);
        $this->assign("page", $list->render());
        return $this->fetch();
    }

    /**
     * 过关斩将数据分析
     * @param RoleModel $roleModel
     * @param PayTotalModel $payTotalModel
     * @return mixed
     */
    public function statGopass(RoleModel $roleModel, PayTotalModel $payTotalModel)
    {
        $sDay = date("Ymd", strtotime(input("sDay", date("Y-m-d"))));
        if($sDay == date("Ymd")){
            try {
                $list = Db::table(config("database.game").".role_gopass")
                    ->alias("rg")
                    ->leftJoin([config("database.game").".role_pay_total" => "rp"], "rp.role_id = rg.role_id")
                    ->where("level", ">", 0)
                    ->group("level")
                    ->column("COUNT(rg.role_id) AS num, COUNT(rp.role_id) AS pay_num", "level");

            }catch (\Exception $e) {
                $list =[];
            }

        }else{
            $list = Db::table("c_stat_gopass")
                ->where("day", $sDay)
                ->column("num, pay_num", "level");
        }
        $keys = array_keys($list);
        $fillArr = array_fill(1, end($keys), 0);
        array_walk($list, function(&$v, $k) use ($fillArr, &$data){
            foreach($fillArr as $i => $vo){
                if($i <= $k){
                    if(isset($data[$i])){
                        $data[$i]["num"] += $v["num"];
                        $data[$i]["pay_num"] += $v["pay_num"];
                    }else{
                        $data[$i] = $v;
                    }
                }else{
                    $data[$i]["num"] = 0;
                    $data[$i]["pay_num"] = 0;
                }
            }
        });
        $this->assign("data", $data);
        $this->assign("total", $roleModel->count("id"));
        $this->assign("payTotal", $payTotalModel->count("role_id"));
        return $this->fetch();
    }

    /**
     * 充值玩家闯关层数
     * @param PayTotalModel $payTotalModel
     * @return mixed
     */
    public function payerGopass(PayTotalModel $payTotalModel)
    {
        $list = $payTotalModel
            ->alias("rp")
            ->join([config("database.game").".role_gopass" => "rg"], "rg.role_id = rp.role_id")
            ->join([config("database.game").".role" => "r"], "r.id = rp.role_id")
            ->field("rp.role_id,rg.level,rp.rmb,r.name")
            ->paginate(self::$pageSize);
        $list->appends($this->request->param());
        $this->assign("data", $list);
        $this->assign("page", $list->render());
        return $this->fetch();
    }
}